const { Controller } = require('egg');

class AwardListController extends Controller {
  async list() {
    const { ctx } = this;
    const { kindId } = ctx.request.body;
    const awardLists = await this.service.awardList.list(kindId);
    this.ctx.helper.success(awardLists);
  }

  async create() {
    const { ctx, service } = this;
    const { avatar, name, title, kindIds, jumpLink  } = ctx.request.body;

    if (!avatar || !name || !kindIds) {
      return this.ctx.helper.error("avatar、name 或 kindIds 不能为空");
    }

    const result = await service.awardList.create({
      avatar,
      name,
      title,
      kindIds,
      jumpLink
    });
    this.ctx.helper.success(result, "获奖记录创建成功");
  }

  async delete() {
    const { ctx, service } = this;
    const { id } = ctx.request.body;

    if (!id) {
      return this.ctx.helper.error("参数异常：缺少 id");
    }
    const result = await service.awardList.delete({ id });
    if (result === 0) {
      return this.ctx.helper.error("删除失败，获奖记录不存在");
    }
    this.ctx.helper.success(result, "获奖记录删除成功");
  }

  async update() {
    const { ctx, service } = this;
    const { avatar, name, title, kindIds, jumpLink, id } = ctx.request.body;

    if (!id) {
      return this.ctx.helper.error("参数异常：缺少获奖记录 ID");
    }
    const result = await service.awardList.update({ id }, { avatar, name, title, kindIds, jumpLink });
    if (result[0] === 0) {
      return this.ctx.helper.error("更新失败，获奖记录不存在");
    }
    this.ctx.helper.success(result, "获奖记录更新成功");
  }
}

module.exports = AwardListController;